package com.zy.thread.product_con;

/**
 * @Author Gong
 * @Description
 * @dATE 2021/12/14 16:26
 **/
public class Clerk {
    private int product=0;
    public synchronized void addProduct(){
        if(product >= 50){
            try {
                this.wait(); //等待不生产产品
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }else{
            product++;
            System.out.println("生产者生产了第"+product+"个产品");
            this.notifyAll();//唤醒所有的等待着
        }

    }
    public synchronized void getProduct(){
        if(product<=0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }else{
            System.out.println("消费者取走了第"+product+"个产品");
            product--;
            this.notifyAll();
        }
    }

}
